/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tomasio.projects.welcome.dao;

import com.tomasio.projects.welcome.dto.PessoaDTO;
import com.tomasio.projects.welcome.dto.VeiculoDTO;
import com.tomasio.projects.welcome.model.Veiculo;
import com.tomasio.projects.welcome.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author administrador
 */
public class HibernateVeiculoDAO implements VeiculoDAO {

    private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    @Override
    public VeiculoDTO find(Long id) {
        SimpleDAOFactory daoFactory = new SimpleDAOFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Veiculo veiculo = (Veiculo) session.get(Veiculo.class, id);
        tx.commit();
        session.close();
        VeiculoDTO veiculoDTO = veiculo.createDTO();
        PessoaDAO dao = daoFactory.getPessoaDAO();
        if (veiculo.getPessoaId() != null && veiculo.getPessoaId() > 0) {
            PessoaDTO pessoaDTO = dao.find(veiculo.getPessoaId());
            veiculoDTO.setPessoa(pessoaDTO);
        }
        return veiculoDTO;
    }

    @Override
    public void create(VeiculoDTO veiculo) {
        Veiculo _veiculo = new Veiculo(veiculo);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(_veiculo);
        tx.commit();
        session.close();

    }

    @Override
    public void remove(Long id) {
        VeiculoDTO dto = find(id);
        Veiculo veiculo = new Veiculo(dto);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.delete(veiculo);
        tx.commit();
        session.close();
    }

    @Override
    public void update(VeiculoDTO veiculo) {
        Veiculo _veiculo = new Veiculo(veiculo);
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.update(_veiculo);
        tx.commit();
        session.close();

    }

    @Override
    public List<VeiculoDTO> findAll() {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        List<Veiculo> veiculos = session.createQuery("from Veiculo veiculo").list();
        tx.commit();
        session.close();

        List<VeiculoDTO> dtoList = new ArrayList<VeiculoDTO>();
        for (Veiculo veiculo : veiculos) {
            dtoList.add(veiculo.createDTO());
        }
        return dtoList;
    }

    @Override
    public List<VeiculoDTO> searchByPlaca(String searchTerm) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        List<Veiculo> veiculos = session.createQuery("from Veciulo veiculo where veiculo.placa like '%" + searchTerm + "%' order by veiculo.placa").list();
        tx.commit();
        session.close();

        List<VeiculoDTO> dtoList = new ArrayList<VeiculoDTO>();
        for (Veiculo veiculo : veiculos) {
            dtoList.add(veiculo.createDTO());
        }
        return dtoList;

    }

    @Override
    public VeiculoDTO findByPlaca(String placa) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Veiculo veiculo = (Veiculo) session.createQuery("from Veiculo veiculo where veiculo.placa = '" + placa + "'").uniqueResult();
        tx.commit();
        session.close();
        if (veiculo != null) {
            return veiculo.createDTO();
        } else {
            return null;
        }

    }
}
